/**
 * 订单成功选择银行去支付功能
 * yangyong
 * 2015-01-08
 * */
define(function(require){
	var $=require("jq");
	var Dialog = require("/front/js/pz_use_dialog.js");
	var css = require("/front/css/pz-dialog.css");
	
	/**
	 * 切换选项卡效果
	 * */
	
	var cardBars     = $("#bank-pay-wrap").find("div.bank-card-bar"),
	    selectedBank = $("#bank-pay-wrap").find("li.selected-bank");
	
	cardBars.delegate("li.select-bank,div.bar-arrow","click",function(){
		var cardWrap = $(this).parentsUntil("#bank-pay-wrap").last();
		//如果是箭头就toggle
		if($(this).hasClass("bar-arrow")){
			cardWrap.toggleClass("bank-card-slidedown").siblings().removeClass("bank-card-slidedown");
		}else{
			cardWrap.addClass("bank-card-slidedown").siblings().removeClass("bank-card-slidedown");
		};
		//如果内部有已选中的银行就自动添加默认选框bank-card.selected
		if(cardWrap.find(":radio:checked")[0]){
			cardWrap.addClass("bank-card-selected").siblings().removeClass("bank-card-selected");
		};
	});
	
	
	/**
	 * 网银在线选择标记已选
	 * */
	var wangYinRadios    = $("#bank-pay-wrap").find(":radio"),
		selectedBankImg  = selectedBank.find("img"),
		selectedBankType = selectedBank.find("sub");
	//提交表单数据
	var submitFm     = $("#js-orderPayForm"),
	    bankcardtype = $("#js-bankcardtype"),
	    banktype     = $("#js-banktype"),
	    bankid       = $("#js-bankId");
	
	wangYinRadios.click(function(){
		
		//如果是网银按钮，标记选中
		if($(this).attr("data-type")==="wangyin"){
			var txt    = $(this).attr("data-card-type")==="yinhangka"?"银行卡":"信用卡",
				imgSrc = $(this).next().find("img").prop("src");
			selectedBankImg.attr("src",imgSrc);
			selectedBankType.text("("+txt+")");
		};
		
		//设置对应银行类型和卡类型到提交表单
		var bankType     = $(this).attr("data-type"),
			bankCardType = $(this).attr("data-card-type"),
			bankID       = $(this).val();
		//设置表单值
		bankid.val(bankID);
		banktype.val(bankType);
		bankcardtype.val(bankCardType);
	});
	
	/**
	 * 去支付
	 * */

	$("#go-pay a").click(function(){

		if(bankcardtype.val()&&banktype.val()){
			//if->1如果不是微信或支付宝
			if(banktype.val()!="qrpay"){
				var d = new Dialog({
					type : "confirm",
					width : 200,
					height:110,
					text : "提示信息",
					tipsText : "<p align='center'>是否确认支付？</p>",
					buttoncancelText : "取消",
					buttonSureText : "确认",
					callBack:function(){
						submitFm[0].submit();	
					},
					mask : 0.3,
					isDrag : true
				});	
			}
			else{//if->1如果不是微信或支付宝
				
				
				
//				var aliascode = $("#js-wx-aliascode").attr("data-aliascode");
//				//如果是支付宝
//				if(bankcardtype.val() == "zhifubao"){
//					window.location = "/ZFBPc/pay.do?aliasCodes="+aliascode;
//				
//				//如果是微信
//				}
//				else if(bankcardtype.val() == "weixin"){
//					
//					$.get("/codePay/WXCodePay.do",{aliasCodes:aliascode},function(ret){
//						
//						if(ret.success){
//							var url = ret.codeUrl;
//							var qr = qrcode(10, 'M');
//							qr.addData(url);
//							qr.make();
//							
//							var wxWin = new Dialog({
//								 type:"alert",
//								  height:500,
//								  width:900,
//								  text:"请打开微信扫一扫功能！",
//								  tipsText:'<div id="weixin-qr">'+qr.createImgTag()+'</div>',
//								  buttonSureText:"关闭", 
//								  callBack:function(){        
//										window.clearTimeout(weixinQRTimer);
//								  },
//								  mask:0.5,
//								  isDrag:true 
//							});
//							
//							//定时监控
//							veridateState(aliascode);
//
//						}else{
//							var wxWin = new Dialog({
//								  type:"alert",
//								  text:"提示",
//								  tipsText:ret.msg,
//								  buttonSureText:"知道了", 
//								  callBack:function(){        
//										
//								  },
//								  mask:0.5,
//								  isDrag:true 
//							});
//						};
//
//					},"json");
//
//				};
			};
		}else{
			var d = new Dialog({
				type : "alert",
				width : 200,
				height:90,
				text : "提示信息",
				tipsText : "<p align='center'>请选择支付银行<p>",
				buttonSureText : "好的",
				mask : 0.3,
				isDrag : true
				// 开起拖动
			});	
		};
	});	
	
	
	
	//定时监控扫一扫
//	var weixinQRTimer = null;
//	var veridateState = function(aliascode){
//		
//		weixinQRTimer = window.setTimeout(function(){
//			
//			$.get("/codePay/payResult.do",{aliasCodes:aliascode},function(ret){
//				
//				if(ret.isPay == 1){
//					window.clearTimeout(weixinQRTimer);
//					window.location = "/pinzhiV2.0/orderResult/wxPaySuccess.jsp";
//				}else{
//					window.clearTimeout(weixinQRTimer);
//					veridateState(aliascode);
//				};
//				
//			},"json");
//			
//		},3000);
//		
//	};
	

	//初始话，上一次选择的信息
	var radio = $(".have-select-bank input:radio:checked");
	if(radio.length<1){
		radio = $(".have-select-bank input:radio").eq(0);
	};
	radio.click();
	

	(function(){
		try{
			var aliasCodes = $("#order-aliasOrder-show span:eq(0)").html().replace(/[^0-9,]/g,"");		
			var as = aliasCodes.split(",");
			if(as.length>=5){
				var payMode = $("#pay-mode").html();
				var totalPrice = $("#order-total-price").html();
				var html = "<span>订单号：<span><br>";
				for(var i=0,l=as.length;i<l;i++){
					//说明是第一个
					if(i%8==0){
						html+="<span>";
					}
					html+=as[i];
					if(i!=l-1){
						html+=",";
					}
					if(i%8==7||i==l-1){
						html+="</span><br>";
					}
				}
				html+="<span>"+payMode+"</span>|<span>应付金额：<strong>"+totalPrice+"</strong>元</span>";
				$("#order-aliasOrder-show").html(html);
			}
		}catch(e){
			
		}
	})();
});